﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using ThinkDotNetEditorLib.Controllers;
using ThinkDotNetEditorLib.Models.StaticObjects;

namespace ThinkDotNetEditorLib.Views
{
    public partial class StaticObjectsView : UserControl,IStaticObjectsView
    {
        private readonly StaticObjectController _controller;
        public StaticObjectsView()
        {
            InitializeComponent();
            _controller = new StaticObjectController(this);            
        }       
        #region Methods

        private void RefreshTabs(IEnumerable<StaticObjectInfo> infos)
        {
            foreach (StaticObjectInfo staticObjectInfo in infos)
            {
                TabPage objectsTabPage = objectsTabControl.TabPages[staticObjectInfo.Category];
                if (objectsTabPage == null)
                    objectsTabPage = CreateCategoryTabPage(staticObjectInfo);

                Image thumbnail = Image.FromFile(staticObjectInfo.ThumbnailPath);
                ToolStripButton toolStripButton = new ToolStripButton(staticObjectInfo.Name, thumbnail);
                toolStripButton.CheckOnClick = true;
                toolStripButton.Tag = staticObjectInfo;
                toolStripButton.Click += ToolStripButtonClick;
                toolStripButton.TextImageRelation = TextImageRelation.TextAboveImage;

                ToolStrip toolStrip = (ToolStrip)objectsTabPage.Controls[0];
                toolStrip.Items.Add(toolStripButton);
            }
        }       
        private TabPage CreateCategoryTabPage(StaticObjectInfo staticObjectInfo)
        {
            ToolStrip objectsList = new ToolStrip();
            TabPage objectsTabPage = new TabPage();
            objectsList.GripStyle = ToolStripGripStyle.Hidden;
            objectsList.LayoutStyle = ToolStripLayoutStyle.Flow;
            objectsList.ImageScalingSize = new Size(64, 64);
            objectsTabPage.Text = objectsTabPage.Name = staticObjectInfo.Category;

            objectsTabControl.TabPages.Add(objectsTabPage);
            objectsTabPage.Controls.Add(objectsList);
            return objectsTabPage;
        }
        private void StaticObjectsView_Load(object sender, System.EventArgs e)
        {
            if (DesignMode == false)
                RefreshTabs(_controller.LoadStaticObjectInfos());
        }
        
        #endregion

        #region Events

        void ToolStripButtonClick(object sender, System.EventArgs e)
        {
            ToolStripButton toolStripButton = (ToolStripButton) sender;
            StaticObjectInfo staticObjectInfo = (StaticObjectInfo)toolStripButton.Tag;
            _controller.SelectedStaticObjectInfo = staticObjectInfo;
        }
        
        #endregion

        #region IStaticObjectsView Members

        public void UncheckObject(StaticObjectInfo staticObjectInfo)
        {
            foreach (TabPage tabPage in objectsTabControl.TabPages)
            {
                ToolStrip toolStrip = (ToolStrip)tabPage.Controls[0];
                foreach (ToolStripButton toolStripButton in toolStrip.Items)
                {
                    if (toolStripButton.Tag == staticObjectInfo)
                        toolStripButton.Checked = false;
                }
            }
        }
        
        #endregion       
    }
}
